<?php
if (!defined("PAST_RECORD_CLASS_PHS")){
	define("PAST_RECORD_CLASS_PHS",1);
	require_once("Constant888.class.php");
	require_once("TodayResult.class.php");
	require_once("DB2XML888.class.php");
	require_once("MyDatabase.class.php");
	$Constant888 = Constant888::instance();	
	class PastRecord {
		private $areaCode;
		private $XML_FileNames;
		
		function PastRecord() {
			global $Constant888;
			$this -> areaCode = $Constant888 -> value("MB_AREA_CODE");
			$this -> XML_FileNames = $this -> fileList();
		}
		
		public function getAreaCode() {
			return $this->areaCode;
		} 
		
		public function showLastNDays($ndays) {			
			
			
			if ( $this -> needUpdate() ) {
				$this -> createXMLFiles();
			}		
			
			foreach($fileNames as $fileName) {
				$this -> showResult($fileName);						
			}
		}		
		
		
		private function needUpdate() {
			//Check date in database , NOT from file ! To fit the codes for MyDatabase888::getLastNDates()
		}
		
		public function createXMLFiles() {
			global $Constant888;
			
			$converter = new DB2XML888($this -> areaCode);
			
			$db888 = new MyDatabase888();			
			
			//Sort : lastest result comes first as $dates[0]
			//die("CHECK date format (in class DB2888 and in database class) ".__FILE__." line: ".__LINE__);
			$dates = $db888 -> getLastNDates($this -> areaCode,$Constant888 -> value("MAX_STORED_DAYS"));
			
			for($i = 0 ; $i < $Constant888 -> value("MAX_STORED_DAYS") ; $i++) {
				$fileName = $this -> XML_FileNames[$i];
				//echo "FILENAME: ",$fileName,"\n";
				$converter -> covert2XML($dates[$i],$fileName);
			}			
			
		}
		
		/**
		 * 
		 * @return mixed returns an array of strings - each is the file name of the stored results of one day
		 * note that: LATE first ! 
		 */
		public function fileList() {
			global $Constant888;
			$arr = array();
			for($i = 0; $i < $Constant888 -> value("MAX_STORED_DAYS") ; $i++) {
				$arr[$i] = $Constant888 -> value("WAP_ROOT_DIR_PATH").'\Data\\'. $Constant888 -> resultFileName($this -> areaCode)."$i".".xml";
			}
			return $arr;
		}
		
		/**
		 * 
		 * @param $fileName an XML file name 
		 * @return nothing just show the file under WML form
		 */
		public function showResult($fileName) {
			global $Constant888; 
			$dailyResult = new TodayResult($fileName);
			$dailyResult -> showResults_WAP();		
			echo '<p>',$Constant888 -> value("SEPARATED_LINE"),'</p>';
		}
		
	}
} 
?>